package com.inifitness.tenant.core.system.service.impl;


import java.util.Arrays;
import java.util.List;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.inifitness.common.model.Option;
import com.inifitness.tenant.core.system.converter.DictItemConverter;
import com.inifitness.tenant.core.system.mapper.SysDictItemMapper;
import com.inifitness.tenant.core.system.model.entity.SysDictItem;
import com.inifitness.tenant.core.system.model.form.DictItemForm;
import com.inifitness.tenant.core.system.model.query.DictItemPageQuery;
import com.inifitness.tenant.core.system.model.vo.DictItemPageVO;
import com.inifitness.tenant.core.system.service.SysDictItemService;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.lang.Assert;
import cn.hutool.core.text.CharSequenceUtil;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;

/**
 * 数据字典项业务实现类
 *
 * @author sunjinfeng
 * @since 2024/04/18 11:00:00
 */
@Service
@RequiredArgsConstructor
public class SysDictItemServiceImpl extends ServiceImpl<SysDictItemMapper, SysDictItem> implements
    SysDictItemService {

  private final DictItemConverter dictItemConverter;

  /**
   * 字典数据项分页列表
   *
   * @param queryParams
   * @return
   */
  @Override
  public Page<DictItemPageVO> getDictItemPage(DictItemPageQuery queryParams) {
    // 查询参数
    int pageNum = queryParams.getPageNum();
    int pageSize = queryParams.getPageSize();
    String keywords = queryParams.getKeywords();
    String typeCode = queryParams.getTypeCode();

    // 查询数据
    Page<SysDictItem> dictItemPage = this.page(
        new Page<>(pageNum, pageSize),
        new LambdaQueryWrapper<SysDictItem>()
            .like(CharSequenceUtil.isNotBlank(keywords), SysDictItem::getName, keywords)
            .eq(CharSequenceUtil.isNotBlank(typeCode), SysDictItem::getTypeCode, typeCode)
            .select(SysDictItem::getId, SysDictItem::getName, SysDictItem::getValue,
                SysDictItem::getStatus)
    );

    // 实体转换
    Page<DictItemPageVO> pageResult = dictItemConverter.entity2Page(dictItemPage);
    return pageResult;
  }

  /**
   * 字典数据项表单详情
   *
   * @param id 字典数据项ID
   * @return
   */
  @Override
  public DictItemForm getDictItemForm(Long id) {
    // 获取entity
    SysDictItem entity = this.getOne(new LambdaQueryWrapper<SysDictItem>()
        .eq(SysDictItem::getId, id)
        .select(
            SysDictItem::getId,
            SysDictItem::getTypeCode,
            SysDictItem::getName,
            SysDictItem::getValue,
            SysDictItem::getStatus,
            SysDictItem::getSort,
            SysDictItem::getRemark
        ));
    Assert.isTrue(entity != null, "字典数据项不存在");

    // 实体转换
    DictItemForm dictItemForm = dictItemConverter.entity2Form(entity);
    return dictItemForm;
  }

  /**
   * 新增字典数据项
   *
   * @param dictItemForm 字典数据项表单
   * @return
   */
  @Override
  public boolean saveDictItem(DictItemForm dictItemForm) {
    // 实体对象转换 form->entity
    SysDictItem entity = dictItemConverter.form2Entity(dictItemForm);
    // 持久化
    boolean result = this.save(entity);
    return result;
  }

  /**
   * 修改字典数据项
   *
   * @param id           字典数据项ID
   * @param dictItemForm 字典数据项表单
   * @return
   */
  @Override
  public boolean updateDictItem(Long id, DictItemForm dictItemForm) {
    SysDictItem entity = dictItemConverter.form2Entity(dictItemForm);
    boolean result = this.updateById(entity);
    return result;
  }

  /**
   * 删除字典数据项
   *
   * @param idsStr 字典数据项ID，多个以英文逗号(,)分割
   * @return
   */
  @Override
  public boolean deleteDictItem(String idsStr) {
    Assert.isTrue(CharSequenceUtil.isNotBlank(idsStr), "删除数据为空");
    //
    List<Long> ids = Arrays.stream(idsStr.split(","))
        .map(Long::parseLong)
        .toList();

    // 删除字典数据项
    boolean result = this.removeByIds(ids);
    return result;
  }

  /**
   * 获取字典下拉列表
   *
   * @param typeCode
   * @return
   */
  @Override
  public List<Option> listDictItemOptions(String typeCode) {
    // 数据字典项
    List<SysDictItem> dictList = this.list(new LambdaQueryWrapper<SysDictItem>()
        .eq(SysDictItem::getTypeCode, typeCode)
        .select(SysDictItem::getValue, SysDictItem::getName)
    );

    // 转换下拉数据
    List<Option> options = CollUtil.emptyIfNull(dictList)
        .stream()
        .map(dictItem -> new Option(dictItem.getValue(), dictItem.getName()))
        .toList();
    return options;
  }
}




